package com.ys.data.api.config;

import com.common.base.BaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.common.pojo.Result;

/**
* @version: V1.0
* @author: szw
* @className: GlobalExceptionHandler
* @packageName: GlobalExceptionHandler
* @description: 全局异常拦截处理类
* @data: 2021-1-19 
**/ 
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public Result logicExceptionHandler(MethodArgumentNotValidException  e) {
        Result result = new Result();
        log.error("系统异常:" + e.getMessage(), e);
        result.setCode(Integer.valueOf(BaseEnum.SERVICE_ERROR.getCode()));
        result.setMsg(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        return result;
    }

}